package com.jinlejia.erp.ui.mine.contract;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jinlejia.erp.R;
import com.wx.wheelview.adapter.ArrayWheelAdapter;
import com.wx.wheelview.widget.WheelView;

import java.util.ArrayList;

/**
 * @author SongJiaTai
 * @description:单项选择PickerView
 * @date :2019/11/8 11:55
 */
public class SheetWheelPickerViewOne extends AlertDialog {
    private ArrayList<String> listMapState;
    private String strTitle;
    private Context mContext;

    protected SheetWheelPickerViewOne(Context context, ArrayList arrayList, String title) {
        super(context);
        this.mContext = context;
        this.listMapState = arrayList;
        this.strTitle = title;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Window window = this.getWindow();
        // *** 主要就是在这里实现这种效果的.
        window.setContentView(R.layout.dialog_picverview_one);
        window.setGravity(Gravity.BOTTOM);
        window.setBackgroundDrawable(new ColorDrawable());

        WindowManager m = window.getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        int fountSize = 30;
        int margin = 35;
        window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, (int) (d.getHeight() * 0.4));

        WheelView.WheelViewStyle style = new WheelView.WheelViewStyle();
        style.selectedTextColor = Color.parseColor("#333333");
        style.textColor = Color.GRAY;
        style.selectedTextSize = 20;
        style.holoBorderColor = Color.GRAY;

        final WheelView wv1 = window.findViewById(R.id.wheelview);
        wv1.setWheelAdapter(new ArrayWheelAdapter(mContext));
        wv1.setWheelData(listMapState);
        wv1.setLoop(true);
        wv1.setSkin(WheelView.Skin.Holo);
        wv1.setStyle(style);
        TextView tvTitle = window.findViewById(R.id.tvTitle);
        tvTitle.setText(strTitle);
        window.findViewById(R.id.cancel).setOnClickListener(v -> dismiss());


        window.findViewById(R.id.confirm).setOnClickListener(v -> {
            if (null != selectListener) {

                selectListener.select(wv1.getSelectionItem().toString());
            }
            dismiss();
        });
    }

    public interface OnSelectListener {
        void select(String state);
    }

    private OnSelectListener selectListener;

    public void setOnSelectListener(OnSelectListener listener) {
        this.selectListener = listener;
    }
}
